package com.haha.redis;

/**
 * @author zouwz5
 * @date 2025/10/3 13:08
 */
public enum RedisKeyEnum {
    USER_REGISTER_BLOOM_FILTER_1("user_register_bloomFilter_1","布隆过滤器键名","用户的手机号","oneOnePlus"),
    USER_REGISTER_BLOOM_FILTER_2("user_register_bloomFilter_1","布隆过滤器键名","用户的手机号","oneOnePlus"),

    // 节目相关
    PROGRAM_SESSIONS_PREFIX("program_sessions","program:sessions:{节目id}","节目的场次信息","oneOnePlus"),


    // 分布式锁名字
    DISTRIBUTEDLOCK_PROGRAM_SESSION("distributedlock_program_session","节目场次信息缓存回写","节目场次信息缓存回写","oneOnePlus")

    ;
    /**
     * key值
     * */
    private final String key;

    /**
     * key的说明
     * */
    private final String keyIntroduce;

    /**
     * value的说明
     * */
    private final String valueIntroduce;

    /**
     * 作者
     * */
    private final String author;

    RedisKeyEnum(String key, String keyIntroduce, String valueIntroduce, String author){
        this.key = key;
        this.keyIntroduce = keyIntroduce;
        this.valueIntroduce = valueIntroduce;
        this.author = author;
    }

    public static String getKey(RedisKeyEnum redisKeyEnum){
        return redisKeyEnum.key;
    }

    // 直接使用实例方法，调用更简洁
    public String getKey() {
        return this.key;
    }

    // 支持动态参数的键生成（针对 PROGRAM_SESSIONS_PREFIX 这样的模板键）
    public String getKey(Object... params) {
        if (params.length == 0) {
            return this.key;
        }
        // 简单的参数替换，例如：program:sessions:{节目id} -> program:sessions:123
        String result = this.key;
        for (Object param : params) {
            result = result + "_" + param.toString();
        }
        return result;
    }


    public static RedisKeyEnum getRedisKeyEnum(String keyCode) {
        for (RedisKeyEnum re : RedisKeyEnum.values()) {
            if (re.key.equals(keyCode)) {
                return re;
            }
        }
        return null;
    }
}
